package com.saifan.wyy_ov.ui.album.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

import com.saifan.wyy_ov.R;
import com.saifan.wyy_ov.ui.album.model.ImageGroup;
import com.saifan.wyy_ov.ui.view.BaseActivity;
import com.saifan.wyy_ov.ui.view.BootstrapButton;
import com.saifan.wyy_ov.utils.ToastUtil;

import java.util.ArrayList;

/**
 * @author WangLi
 * @date 2014年12月5日 上午12:13:23
 * @Description 照片展示页面
 * @version V1.0
 */

public class PhotoShowActivity extends BaseActivity{
    private ImageGroup igp;
    private ArrayList<String> selectList;
    private GridView photo_grid;
    private PhotoShowAdapter adapter;
    private BootstrapButton submit;
    private Toolbar toolbar;
    boolean isSingle=false;
    private int count;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photoshow);
        this.toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        igp = (ImageGroup) getIntent().getSerializableExtra("image");
        selectList = getIntent().getStringArrayListExtra("data");
        isSingle= getIntent().getBooleanExtra("isSingle", false);
        count = getIntent().getIntExtra("count", 5);
        toolbar.setTitle(igp.getDirName());
        submit = (BootstrapButton) findViewById(R.id.photo_submit);
        if (selectList.size() > 0){
            submit.setText(getString(R.string.photo_select_submit, selectList.size()));
        }

        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putStringArrayListExtra("data", selectList);
                setResult(RESULT_OK, intent);
                PhotoShowActivity.this.finish();
            }
        });
        photo_grid = (GridView) findViewById(R.id.photo_show_grid);
        adapter = new PhotoShowAdapter(PhotoShowActivity.this, igp, selectList, photo_grid);

        photo_grid.setAdapter(adapter);

        photo_grid.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String path = igp.getImages().get(position);
                if (isSingle){
                    Intent intent = new Intent();
                    intent.putStringArrayListExtra("data", selectList);
                    selectList.clear();
                    selectList.add(path);
                    setResult(RESULT_OK, intent);
                    PhotoShowActivity.this.finish();

                }else{
                    int index = -1;
                    if (selectList.contains(path)) {
                        selectList.remove(path);
                    } else {
                        if (selectList.size() < count) {
                            selectList.add(path);
                        }
                        else {
                            ToastUtil.showToast(PhotoShowActivity.this, String.format(getString(R.string.photo_select_exceed), count));
                            return;
                        }
                    }
                    index = igp.getImages().indexOf(path);
                    adapter.refreshView(index);
                    if (selectList.size() > 0) {
                        submit.setText(getString(R.string.photo_select_submit, selectList.size()));
                    }
                    else {
                        submit.setText("确定");
                    }
                }

            }
        });
    }
}
